package cc.javaee.bbs.service.activity;

import cc.javaee.bbs.dao.activity.*;
import cc.javaee.bbs.model.activity.KbrActivity;
import cc.javaee.bbs.params.WebParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityTaskService{
	@Autowired
	ActivityDao activityDao;
	
	@Autowired
	ActivityPrizeDao activityPrizeDao;
	@Autowired
	ActivityArrangeDao activityArrangeDao;
	@Autowired
	ActivityFileDao activityFileDao;
	@Autowired
	ActivityCustomerDao activityCustomerDao;
	
	public void checkTime() {
		KbrActivity kbrActivity=new KbrActivity();
//		kbrActivity.setStatus("尚未开始");
		kbrActivity.setStatus(0);
		List<KbrActivity> unList=activityDao.find(kbrActivity);

//		kbrActivity.setStatus("正在进行");
		kbrActivity.setStatus(0);
		List<KbrActivity> ingList=activityDao.find(kbrActivity);
		
		unList.addAll(ingList);
		WebParams param=new WebParams();
		
		if(unList!=null&&unList.size()>0){
			for (KbrActivity kbra : unList) {
				Date enStartTime=kbra.getEnStartTime();
				Date enEndTime=kbra.getEnEndTime();
				Date startTime=kbra.getStartTime();
				Date endTime=kbra.getEndTime();
				 long diffOne = enStartTime.getTime() - (new Date()).getTime();
				 long diffTwo = enEndTime.getTime() - (new Date()).getTime();
				 long diffThree = startTime.getTime() - (new Date()).getTime();
				 long diffFour = endTime.getTime() - (new Date()).getTime();
					if(diffOne>0){
//						kbra.setSignStatus("尚未开始报名");
						kbra.setSignStatus(0);
//						param.put("signStatus", "尚未开始");
						param.put("signStatus", 0);
					}else if(diffOne<0&&diffTwo>0){
//						kbra.setSignStatus("正在报名");
						kbra.setSignStatus(1);
//						param.put("signStatus", "正在报名");
						param.put("signStatus", 1);
					}else if(diffTwo<0){
//						kbra.setSignStatus("报名结束");
						kbra.setSignStatus(2);
//						param.put("signStatus", "报名结束");
						param.put("signStatus", 2);
					}
					if(diffThree>0){
//						kbra.setStatus("尚未开始");
						kbra.setStatus(0);
//						param.put("status", "尚未开始");
						param.put("status", 0);
					}else if(diffThree<0&&diffFour>0){
//						kbra.setStatus("正在进行");
						kbra.setStatus(1);
//						param.put("status", "正在进行");
						param.put("status", 1);
					}else if(diffFour<0){
//						kbra.setStatus("已结束");
						kbra.setStatus(2);
//						param.put("status", "已结束");
						param.put("status", 2);
					}
					param.put("id", kbra.getId());
					activityDao.updateByParam(param);
			}
		}
	}
}
